package com.woniuxy.tms.accept.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.woniuxy.tms.accept.domain.dto.QuerryWaybillDTO;
import com.woniuxy.tms.accept.utils.BeanUtils;
import com.woniuxy.tms.accept.utils.WaybillUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.woniuxy.tms.accept.mapper.WaybillMapper;
import com.woniuxy.tms.accept.domain.Waybill;
import com.woniuxy.tms.accept.service.IWaybillService;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

/**
 * 货运单Service业务层处理
 *
 * @author dxw
 * @date 2022-04-20
 */
@Service
public class WaybillServiceImpl implements IWaybillService {
    @Resource
    private WaybillMapper waybillMapper;

    /**
     * 查询货运单
     *
     * @param waybillId 货运单主键
     * @return 货运单
     */
    @Override
    public Waybill selectWaybillByWaybillId(Long waybillId) {
        return waybillMapper.selectWaybillByWaybillId(waybillId);
    }

    /**
     * 查询货运单列表
     *
     * @param waybill 货运单
     * @return 货运单
     */
    @Override
    public List<Waybill> selectWaybillList(QuerryWaybillDTO waybill) {
        return waybillMapper.selectWaybillList(waybill);
    }

    /**
     * 新增货运单
     *
     * @param waybill 货运单
     * @return 结果
     */
    @Override
    public int insertWaybill(Waybill waybill) {
        //添加操作记录的属性
        Waybill waybill1 = BeanUtils.addAddParams(waybill);
        //计算各种费用
        waybill = WaybillUtils.getVariousFees(waybill);
        waybill=WaybillUtils.unitConversion(waybill);
        //生成货运单编号
        waybill.setWaybillNumber(WaybillUtils.getWaybillNumber());
        return waybillMapper.insertWaybill(waybill);
    }

    /**
     * 修改货运单
     *
     * @param waybill 货运单
     * @return 结果
     */
    @Override
    public int updateWaybill(Waybill waybill) {
        waybill = BeanUtils.updateAddParams(waybill);
        waybill = WaybillUtils.getVariousFees(waybill);
        //不需要代收服务
        if(waybill.getIsNeedCollectPayment()==0){
            //反款类型,设置
            waybill.setRefundType(0);
        }
        return waybillMapper.updateWaybill(waybill);
    }

    /**
     * 批量删除货运单
     *
     * @param waybillIds 需要删除的货运单主键
     * @return 结果
     */
    @Override
    public int deleteWaybillByWaybillIds(Long[] waybillIds) {
        Long updatedBy = SecurityUtils.getUserId();
        Date updatedTime=new Date();
        return waybillMapper.deleteWaybillByWaybillIds(waybillIds,updatedBy,updatedTime);
    }

    /**
     * 删除货运单信息
     *
     * @param waybillId 货运单主键
     * @return 结果
     */
    @Override
    public int deleteWaybillByWaybillId(Long waybillId) {
        Long updatedBy = SecurityUtils.getUserId();
        Date updatedTime=new Date();
        return waybillMapper.deleteWaybillByWaybillId(waybillId,updatedBy,updatedTime);
    }
}
